package com.dayuanit.dy8.atm.springbootatm.controller;

import com.dayuanit.dy8.atm.springbootatm.dto.ResponseDTO;
import com.dayuanit.dy8.atm.springbootatm.entity.User;
import com.dayuanit.dy8.atm.springbootatm.exception.BizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

public class BaseController {

    protected static Logger logger = LoggerFactory.getLogger(BaseController.class);

    @Value("${file.avatar.path}")
    protected String avatarFilePath;

    @ExceptionHandler(BizException.class)
    @ResponseBody
    public ResponseDTO processBizException(BizException be) {
        be.printStackTrace();
        return ResponseDTO.fail(be.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseDTO processException(Exception e) {
        e.printStackTrace();
        return ResponseDTO.fail("请联系客服");
    }

    public int getUserId(HttpSession session) {
        Object obj = session.getAttribute("loginUser");
        if (null == obj) {
            throw new BizException("请先登录");
        }

        User user = (User)obj;
        return user.getId();

//        return 1000;
    }
}
